//
//  ViewController.m
//  05-UIScrollView
//
//  Created by 曹曹 on 16/7/20.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"

// 宏定义, 命名的两种方式:
// 宏嵌套
// 宏进行系统编译时, 代码的替换(预编译指令)
#define kScreenBounds [UIScreen mainScreen].bounds
// 全部大写
#define SCREEN_WIDTH kScreenBounds.size.width
#define SCREEN_HEIGHT kScreenBounds.size.height
// 小写的k加驼峰命名
#define kWidth kScreenBounds.size.width
#define kHeight kScreenBounds.size.height
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:kScreenBounds];
    [self.view addSubview:scrollView];
    scrollView.backgroundColor = [UIColor orangeColor];
    // 1.contentSize, 内容的范围 默认CGSizeZero
    // 设置可以滚动的范围
    scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 5, SCREEN_HEIGHT * 4);
    // 2.contentOffset, 默认CGPointZero
    // 初始的偏移量
    // 不设置contentOffset也可以改变它的值
    // 问题: 改变contentOffset, 什么东西变了
    // 答案: 改变contentOffset, 实质上是对scrollView的bounds进行修改
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(20, 50, SCREEN_WIDTH - 40, 100);
    [button setTitle:@"button" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor purpleColor];
    [scrollView addSubview:button];
    NSLog(@"frame is :%@", NSStringFromCGRect(scrollView.frame));
    NSLog(@"bounds is :%@", NSStringFromCGRect(scrollView.bounds));
    scrollView.contentOffset = CGPointMake(0, 500);
    NSLog(@"frame now is :%@", NSStringFromCGRect(scrollView.frame));
    NSLog(@"bounds now is :%@", NSStringFromCGRect(scrollView.bounds));
    // 3.contentInset, 设置内容的 内偏移量
//    scrollView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    // 4.directionalLockEnabled, 锁定垂直/水平滚动 45°移动时锁失效
    scrollView.directionalLockEnabled = YES;
    // 5.bounces, 回弹效果(边缘动画) 默认开启
//    scrollView.bounces = NO;
    // 6.alwaysBounceVertical, 在没有垂直方向的滚动范围时, 开启垂直方向的回弹效果(当bounces关闭时此属性无效)
    scrollView.alwaysBounceVertical = YES;
    // 7.
    scrollView.alwaysBounceHorizontal = YES;
    // 8.pagingEnabled, 按页滚动, 默认NO
    scrollView.pagingEnabled = NO;
    // 9.scrollEnabled, 控制能否滚动, 默认YES
    scrollView.scrollEnabled = YES;
    // 10.控制是否显示垂直/水平的滚动指示器, 默认都为YES
    scrollView.showsHorizontalScrollIndicator = YES;
    scrollView.showsVerticalScrollIndicator = YES;
    // 11.滚动指示器距屏幕边缘的内偏移(距右侧/底部的距离)
//    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(100, 100, 100, 100);
    // 12.indicatorStyle, 滚动指示器的颜色(枚举值)
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    // 13.isTracking, 用户是否开始拖动
    //BOOL isTracking =  scrollView.tracking;
    // isDragging, 用户是否正在拖动
    //BOOL isDragging =  scrollView.dragging;
    // isDecelerating, 是否正在减速
    //BOOL isDecelerating = scrollView.decelerating;
    // 14.scrollsToTop, 点击状态栏回到视图顶部手势是否存在, 默认为YES
    scrollView.scrollsToTop = YES;
    // 15.
    scrollView.delegate = self;
    // 16.keyboardDismissMode, 键盘消失模式
    UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 200, 40)];
    text.backgroundColor = [UIColor greenColor];
    text.placeholder = @"请输入";
    [scrollView addSubview:text];
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    // 17.缩放系数
    scrollView.minimumZoomScale = 0.5;
    scrollView.maximumZoomScale = 2;
}
#pragma mark - scrollViewDelegate
// 如果想实现视图的缩放, 必须实现viewForZoomingInScrollView:方法
// 与想要缩放的子视图绑定, scrollView的contentSize会跟随子视图改变
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return scrollView.subviews.firstObject;
}
// 滚动视图一滚动就执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // tracking 用户触摸
    if (scrollView.tracking) {
        NSLog(@"拖动滚动中");
    }
    // dragging 用户开始滑动(按页滚动时准确)
    if (scrollView.dragging) {
        NSLog(@"");
    }
    // decelerating 用户触摸结束
    if (scrollView.decelerating) {
        NSLog(@"");
    }
}
// 将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"开始拖拽");
}
// 已经结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    NSLog(@"结束拖拽");
}
// 将要开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    NSLog(@"开始减速");
}
// 已经结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSLog(@"结束减速");
}

#pragma mark - Other
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
